<?php
/**
 * Created by PhpStorm.FUN-AC-0021-浦发银行五折购
 * User: allen.qiang
 * Date: 14-12-19
 * Time: 上午10:50
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityPf {

    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  20141219 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master(&$arg_array){
        global $G_SHOP;

        //实例化配置类
        $activity_config = new activityConfig('PF2015'); //浦发银行五折购活动配置
        $config_array = $activity_config->config;

        $isbool=false;
        //sql字符串
        $sql = ' ';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //会员名
        if (isset($arg_array['custID']) && is_numeric($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['custID']);
            $isbool=true;
        }

        //会员ID
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['pcustID'];
            $isbool=true;
        }
        //促销渠道ID
        if (isset($config_array['billPromotion']) && !empty($config_array['billPromotion'])) {
            $sql .= ' AND billPromotion=?';
            $params_type .= 's';
            $params_array[] = $config_array['billPromotion'];
        }
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到浦发银行五折购使用状态 0：未使用，1：已使用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $status=0;
       if($isbool){
           $sql_master="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' ".$sql;
           $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master,$params_type,$params_array);
           $item_count = $temp_item_master[0]->item_count;
           if($item_count>0){ //已使用浦发银行五折购
               $status=1;
           }
       }

        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动的每日蛋糕数量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' AND billPromotion=".$config_array['billPromotion'];
        $temp_item_master2 = &$G_SHOP->DBCA->stmt_query($sql_master2);
        $sql_master3="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00',strtotime('-1 day'))."' AND createTime<'".date('Y-m-d 23:59:59',strtotime('-1 day'))."' AND billPromotion=".$config_array['billPromotion'];
        $temp_item_master3 = &$G_SHOP->DBCA->stmt_query($sql_master3);
        $number=$config_array['cake_sum'];
        if($temp_item_master3[0]->item_count>$number){
            $number =$number- ($temp_item_master3[0]->item_count-$number);
        }
        if(date('Y-m-d')==date('2015-03-14')){
            $number=$number+80;
        }
        if($temp_item_master2){
            $number =$number- $temp_item_master2[0]->item_count;
        }

        if($number<0)
            $number=0;

        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<是否在活动时间内 0：未在活动时间内；1：在活动时间内>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $isDate=0;
        $Message="";
        $title="";

        /**
         * 浦发银行周末活动结束，暂时注释掉，改甜蜜星期四活动，
         * @editer allen.qiang 2015-03-26
        if(strstr($config_array["activity_date"],date('Y-m-d')) && date('Y-m-d H:i:s')<=$config_array["endTime"] && date('Y-m-d H:i:s')>=date('Y-m-d 11:00:00')){
            $isDate=1;
            $dateTime_arry=explode(',',$config_array["dateTime_arry"]);
            $titleDate_arry=explode(',',$config_array["titleDate_arry"]);
            for($i=0;$i<count($dateTime_arry);$i++){
                $j=count($dateTime_arry)-1-$i;
                $dateTime = date('Y-m-d' ,strtotime($dateTime_arry[$j])); //判断下次订购时间的截止日期
                if(date('Y-m-d')>$dateTime){
                    $title=$titleDate_arry[$j];
                    break;
                }
            }
        }else{
            $dateTime_arry=explode(',',$config_array["dateTime_arry"]);
            $messageDate_arry=explode(',',$config_array["messageDate_arry"]);
            $titleDate_arry=explode(',',$config_array["titleDate_arry"]);
            for($i=0;$i<count($dateTime_arry);$i++){
                $j=count($dateTime_arry)-1-$i;
                $dateTime = date('Y-m-d' ,strtotime($dateTime_arry[$j])); //判断下次订购时间的截止日期
                 if(date('Y-m-d')>$dateTime){
                     $Message=$messageDate_arry[$j];
                     $title=$titleDate_arry[$j];
                     break;
                 }
            }
        }
        */

        /**
         * MK-AC-1005-BD2015年浦发银行半价活动
         * @editer allen.qiang 2015-03-26
         */

        if(strstr($config_array["activity_date"],date('Y-m-d')) && date('Y-m-d H:i:s')<=$config_array["endTime"] && date('Y-m-d H:i:s')>=date('Y-m-d '.$config_array["start_time"]) && date('Y-m-d H:i:s')<=date('Y-m-d '.$config_array["end_time"])){
            $isDate=1;
            $title=date('Y-m-d').' '.$config_array["start_time"].' ~ '.date('Y-m-d').' '.$config_array["end_time"];
        }else{
            $messageDate_arry=explode(',',$config_array["messageDate_arry"]);
            $activity_date_arry=explode(',',$config_array["activity_date"]);
            for($i=0;$i<count($activity_date_arry);$i++){
                $dateTime = $activity_date_arry[$i].' '.$config_array['start_time']; //判断下次订购时间的截止日期
                if(date('Y-m-d h:i:s')<$dateTime){
                    $Message=$messageDate_arry[$i].$config_array["start_time"].'~'.$config_array["end_time"];
                    $title=$activity_date_arry[$i].' '.$config_array["start_time"].' ~ '.$activity_date_arry[$i].' '.$config_array["end_time"];
                    break;
                }
            }
        }


        //用户信息数组
        $activity_array = array(
            'billPromotion' => $config_array['billPromotion'],//促销渠道ID
            'activity_status'	     => $status,//使用状态
            'endtime'	     => $config_array['endTime'],//活动结束时间
            'activity_num' => $number,//每日蛋糕所剩数量
            'isDate'        => $isDate,//是否在活动时间内
            'send_end_time' => $config_array["send_end_time"], //配送结束时间
            'activity_date' => $config_array["activity_date"], //活动的订购日期
            'paytypeID' => $config_array["paytypeID"], //在线银行浦发银行paytypeID
            'message' => $Message, //下次活动提示时间
            'titleDate' => $title //每次显示的订购时间
        );

        return $activity_array;

    }


    /**
     * 记录页面上【浦发银行卡申请】按钮的点击次数
     * @return null|array
     * @author  20141219 allen.qiang <allen.qiang@fistforward.com>
     */
    public function pf_yhk_log($sku){
        global $G_SHOP;

        try {
            $G_SHOP->DBCA->beginTransaction();

            $activityLog = new entity('activity_log');
            $activityLog->creator = $G_SHOP->currentCustomerID==""?$this->getIP():$G_SHOP->currentCustomerID;
            $activityLog->pcustID = $G_SHOP->currentPCustID;
            $activityLog->sku = $sku;

            $G_SHOP->DBCA->save($activityLog) ;
            $G_SHOP->DBCA->commit();
            return array( 'code' => 200, 'message' => '记录成功' );
        } catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }
    }

    /**
     * 查询IP
     * @return $realip
     * @author  20141219 allen.qiang <allen.qiang@fistforward.com>
     */
    public function getIP()
    {
        static $realip;
        if (isset($_SERVER)){
            if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
                $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
            } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
                $realip = $_SERVER["HTTP_CLIENT_IP"];
            } else {
                $realip = $_SERVER["REMOTE_ADDR"];
            }
        } else {
            if (getenv("HTTP_X_FORWARDED_FOR")){
                $realip = getenv("HTTP_X_FORWARDED_FOR");
            } else if (getenv("HTTP_CLIENT_IP")) {
                $realip = getenv("HTTP_CLIENT_IP");
            } else {
                $realip = getenv("REMOTE_ADDR");
            }
        }
        return $realip;
    }


} 